import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress'; // 顶部加载进度条
import 'nprogress/nprogress.css'; // 进度条样式
import {getToken} from '@/utils/auth'; // 获取token

import Login from './views/Login.vue';
// import Register from './views/Register/index';

import {QR} from '@/api/login'

Vue.use(Router);

import Layout from '@/views/Layout/Layout';
const router = new Router({
    mode: 'history',
    scrollBehavior: () => ({y: 0}),
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: {title: '登录', main: false}
        },
        {
            path: '/analysis',
            component: Layout,
            redirect: '/analysis/index',
            meta: {title: '数据分析', main: true, icon: 'shujufenxi'},
            children: [
                {
                    path: 'index',
                    name: 'Analysis',
                    component: () => import('@/views/Analysis/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/',
            component: Layout,
            redirect: 'orderlist',
            meta: {title: '订单管理', main: true, icon: 'dingdan1'},
            children: [
                {
                    path: 'orderlist',
                    name: 'Orderlist',
                    component: () => import('@/views/Order/index'),
                    meta: {title: ''}
                },
                {
                    path: 'ordercreate',
                    name: 'orderCreate',
                    component: () => import('@/views/Order/orderCreate'),
                    meta: {title: '订单处理'}
                }
            ]
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/views/Register/index'),
            meta: {title: '注册激活', main: false}
        },
        {
            path: '/filing',
            component: Layout,
            redirect: '/filing/index',
            meta: {title: '企业备案', main: true, icon: 'beian1'},
            children: [
                {
                    path: 'index',
                    name: 'filing',
                    component: () => import('@/views/filing/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/setting',
            component: Layout,
            redirect: '/setting/index',
            meta: {title: '系统设置', main: true, icon: 'shezhi-'},
            children: [
                {
                    path: 'index',
                    name: 'Setting',
                    component: () => import('@/views/Setting/index'),
                    meta: {title: ''}
                }
            ]
        },

        {
            path: '/market',
            component: Layout,
            redirect:'/market/index',
            /*延伸服务*/
            meta: {title: '延伸服务', main: true, icon: 'applications'},
            children: [
                {
                    path: 'index',
                    name: 'market',
                    component: () => import('@/views/Market/index'),
                    meta: {title: ''}
                },
                {
                    path: 'append',
                    name: 'append',
                    component: () => import('@/views/Market/append'),
                    meta: {title: ''}
                }
            ]
        }, {
            path: '/survey',
            redirect: '/survey/index',
            component: Layout,
            meta: {title: '问卷调查', main: true, icon: 'wenjuandiaocha'},
            children: [
                {
                    path: 'index',
                    name: 'survey',
                    component: () => import('@/views/survey/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/feedback',
            redirect: '/feedback/index',
            component: Layout,
            meta: {title: '意见反馈', main: true, icon: 'wenjuan'},
            children: [
                {
                    path: 'index',
                    name: 'feedback',
                    component: () => import('@/views/feedback/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/register/step2',
            name: 'registerStep2',
            component: () => import('@/views/Register/step2'),
            meta: {title: '企业信息完善', main: false}
        },
        {
            path: '/password',
            name: 'Password',
            component: () => import('@/views/Password/index'),
            meta: {title: '通过手机找回密码', main: false}
        }

    ]
});

// 设置需要排除的路由
let exRouterReg = /\/(register|password)/;

router.beforeEach((to, from, next) => {
    NProgress.start();
    let token = localStorage.getItem('token')
    let check = localStorage.getItem('check');
    if (to.path == '/register') {
        next();
        return
    };
    if (token) {
        /* 如果token存在 */
        check = localStorage.getItem('company-check');

            QR(token).then(res=>{
                if(res.code ==0){
                    //游客
                    if (to.path == '/login' || exRouterReg.test(to.path)) {
                        next(to.path);
                        console.log(to.path)
                        NProgress.done();
                    }else{
                        next('/login')
                    }
                }else{
                    //企业
                    //res.code ==1
                    localStorage.setItem('company-check',true)
                    token = localStorage.getItem('token')
                    if (to.path == '/login') {
                        /* 拦截去login界面的路由到有权限的默认首页 */
                        next({path: '/'});
                        NProgress.done();
                    };
                    if (exRouterReg.test(to.path)) {
                        next();
                        NProgress.done();
                    }
                }

            }).catch(err=>{
                console.log(err)
            })
        next();
    } else {
        if (to.path == '/login' || exRouterReg.test(to.path)) {
            next();
        } else {
            next('/login');
        }
    }
});

router.afterEach(() => {
    NProgress.done(); // finish progress bar
});

export default router;
